package com.mbyte.easy.taskclass.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbyte.easy.taskclass.entity.TaskClass;
import com.mbyte.easy.taskclass.service.ITaskClassService;
import com.mbyte.easy.common.controller.BaseController;
import com.mbyte.easy.common.web.AjaxResult;
import com.mbyte.easy.taskstudent.entity.TaskStudent;
import com.mbyte.easy.taskstudent.service.TaskIStudentService;
import com.mbyte.easy.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.ObjectUtils;
import java.util.List;

/**
* <p>
* 前端控制器
* </p>
* @author lp
* @since 2019-03-11
*/
@Controller
@RequestMapping("/taskclass/qcbyClass")
public class TaskClassController extends BaseController  {

    private String prefix = "taskclass/qcbyClass/";



    @Autowired
    private ITaskClassService qcbyClassService;

    @Autowired
    private TaskIStudentService studentService;

    /**
    * 查询列表
    *
    * @param model
    * @param pageNo
    * @param pageSize
    * @param qcbyClass
    * @return
    */
    @RequestMapping
    public String index(Model model,@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, String createTimeSpace, String deleteTimeSpace, TaskClass qcbyClass) {
        Page<TaskClass> page = new Page<TaskClass>(pageNo, pageSize);
        QueryWrapper<TaskClass> queryWrapper = new QueryWrapper<TaskClass>();
        if(!ObjectUtils.isEmpty(qcbyClass.getClassName())) {
            queryWrapper = queryWrapper.like("class_name",qcbyClass.getClassName());
         }
        if(!ObjectUtils.isEmpty(qcbyClass.getDeleteTime())) {
            queryWrapper = queryWrapper.like("delete_time",qcbyClass.getDeleteTime());
         }
        if(!ObjectUtils.isEmpty(qcbyClass.getDeleteStatus())) {
            queryWrapper = queryWrapper.like("delete_status",qcbyClass.getDeleteStatus());
         }
        IPage<TaskClass> pageInfo = qcbyClassService.page(page, queryWrapper);
        model.addAttribute("deleteTimeSpace", deleteTimeSpace);
        model.addAttribute("searchInfo", qcbyClass);
        model.addAttribute("pageInfo", new PageInfo(pageInfo));
        return prefix+"list";
    }

    /**
    * 添加跳转页面
    * @return
    */
    @GetMapping("addBefore")
    public String addBefore(){
        return prefix+"add";
    }



    /**
    * 添加
    * @param qcbyClass
    * @return
    */
    @PostMapping("add")
    @ResponseBody
    public AjaxResult add(TaskClass qcbyClass){
        return toAjax(qcbyClassService.save(qcbyClass));
    }
    /**
     * 查看跳转页面
     * @return
     */
    private String prefix1 = "taskstudent/student/";
    @GetMapping("editBefore/{id}")
    public String editBefore(Model model,@PathVariable("id")Long id,@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, TaskStudent student){
        Page<TaskStudent> page = new Page<TaskStudent>(pageNo, pageSize);
        QueryWrapper<TaskStudent> queryWrapper = new QueryWrapper<TaskStudent>();
        if(!ObjectUtils.isEmpty(student.getUserId())) {
            queryWrapper = queryWrapper.like("user_id",student.getUserId());
        }
        if(!ObjectUtils.isEmpty(student.getClassId())) {
            queryWrapper = queryWrapper.like("class_id",student.getClassId());
        }
        if(!ObjectUtils.isEmpty(student.getName())) {
            queryWrapper = queryWrapper.like("name",student.getName());
        }
        if(!ObjectUtils.isEmpty(student.getSex())) {
            queryWrapper = queryWrapper.like("sex",student.getSex());
        }
        if(!ObjectUtils.isEmpty(student.getAge())) {
            queryWrapper = queryWrapper.like("age",student.getAge());
        }
        if(!ObjectUtils.isEmpty(student.getPhone())) {
            queryWrapper = queryWrapper.like("phone",student.getPhone());
        }
        if(!ObjectUtils.isEmpty(student.getEmail())) {
            queryWrapper = queryWrapper.like("email",student.getEmail());
        }
        IPage<TaskStudent> pageInfo = studentService.getByClassId(page, queryWrapper,id);
        model.addAttribute("searchInfo", student);
        model.addAttribute("pageInfo", new PageInfo(pageInfo));
        //model.addAttribute("student",studentService.getByClassId(id));
        return prefix1+"list";
    }
    /**
    * 添加
    * @param qcbyClass
    * @return
    */
    @PostMapping("edit")
    @ResponseBody
    public AjaxResult edit(TaskClass qcbyClass){

        return toAjax(qcbyClassService.updateById(qcbyClass));

    }
    /**
    * 删除
    * @param id
    * @return
    */
    @GetMapping("delete/{id}")
    @ResponseBody
    public AjaxResult delete(@PathVariable("id") Long id){
        return toAjax(qcbyClassService.removeById(id));
    }
    /**
    * 批量删除
    * @param ids
    * @return
    */
    @PostMapping("deleteAll")
    @ResponseBody
    public AjaxResult deleteAll(@RequestBody List<Long> ids){
        return toAjax(qcbyClassService.removeByIds(ids));
    }

}

